﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;

namespace SocketClient
{
    /// <summary>
    /// Interaction logic for visualizarsala.xaml
    /// </summary>
    public partial class visualizarsala : Window
    {
        private Conexao conexao;
        private int j;

        public visualizarsala(Conexao con)
        {
            this.conexao = con;
            //writer.WriteLine("visualizarsala    ");
            conexao.Send("visualizarsala/");

            //String txt = reader.ReadLine();
            String txt = con.Receive();
            string[] salas = txt.Split('|');

            InitializeComponent();
            if (txt != "")
            {
                foreach (string s in salas)
                {
                    listBox1.Items.Add(s);
                }
            }
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            String nomeSala = listBox1.SelectedItem.ToString();
            //writer.WriteLine("entrarsala    " + nomeSala); //envia msg ao servidor
            conexao.Send("entrarsala/" + nomeSala);
            //String resposta = reader.ReadLine();
            String resposta = conexao.Receive();
            String[] tokens = resposta.Split('|');
            if (tokens[2] == "8")
            {
                MessageBox.Show("sala lotada!");
            }
            else if (tokens[0] == nomeSala)
            {
                salinha s = new salinha(conexao, tokens[1], tokens[0]);
                Close();
                s.ShowDialog();
            }
            else
            {
                MessageBox.Show("Resposta do servidor: " + resposta);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Clear();
            //writer.WriteLine("atualizarsala    ");
            conexao.Send("atualizarsala/");

            //String txt = reader.ReadLine();
            String txt = conexao.Receive();
            string[] salas = txt.Split('|');

            if (txt != "")
            {
                foreach (string s in salas)
                {
                    listBox1.Items.Add(s);
                }
            }
        }

    }
}
